// pages/auth/register/register.js
const app = getApp();
const api = require("../../../config/api.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    usernm: "",
    passwd1: "",
    passwd2: "",
    phoneno: ""
  },
  requestRegister: function (wxCode) {
    let that = this;
    wx.request({
      url: api.AuthRegister,
      data: {
        username: that.data.usernm,
        password: that.data.passwd1,
        mobile: that.data.phoneno,
        code: "1234",
        wxCode: wxCode
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.errno == 0) {
          app.globalData.hasLogin = true;
          wx.setStorageSync('userInfo', res.data.data.userInfo);
          wx.setStorage({
            key: "token",
            data: res.data.data.token,
            success: function () {
              wx.switchTab({
                url: '/pages/user/user'
              });
            }
          });
        } else {
          wx.showModal({
            title: '错误信息',
            content: res.data.errmsg,
            showCancel: false
          });
        }
      }
    });
  },
  inputName: function (e) {
    this.setData({
      usernm: e.detail.value
    })
  },
  inputPasswd1: function (e) {
    this.setData({
      passwd1: e.detail.value
    })
  },
  inputPasswd2: function (e) {
    this.setData({
      passwd2: e.detail.value
    })
  },
  inputPhone: function (e) {
    this.setData({
      phoneno: e.detail.value
    })
  },
  registerFn: function () {
    let that = this;
    let flag = this.data.usernm != "" && this.data.passwd1 != "" && this.data.passwd2 != "" && this.data.phoneno != '';
    if (!flag) {
      wx.showToast({
        icon: "none",
        title: '输入框不能为空',
      })
      return
    }
    let psswdflag = this.data.passwd1 === this.data.passwd2 ? true : false;
    if (!psswdflag) {
      wx.showToast({
        title: '两次密码不一致',
        icon: "none",
      })
    }
    else {
      wx.login({
        success: function (res) {
          if (!res.code) {
            wx.showModal({
              title: '错误信息',
              content: '注册失败',
              showCancel: false
            });
          }
          that.requestRegister(res.code);
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})